$(function(){
	$("strong[tag='adjustPriceSpan']").click(function(){
		var id = $(this).find("input[name='id']").val();
		var imagePath = $(this).find("input[name='imagePath']").val();
		var productName = $(this).find("input[name='productName']").val();
		//原价
		var productPrice = $(this).find("input[name='productPrice']").val();
		//现价
		var adjustPorductPrice = $(this).find("input[name='adjustProductPrice']").val();
		//数量
		var buyNum = $(this).find("input[name='buyNum']").val();
		//已调整价格
		var adjustPrice = subtraction(productPrice, adjustPorductPrice);
		adjustPrice = adjustPrice ? adjustPrice : "";
		
		//商品已调整价格
		$("#adjustPrice").val(adjustPrice);
		//商品id
		$("#detailId").val(id);
		//商品图片
		$("#imagePath").attr("src",imgRootURL+"/"+imagePath);
		//商品名称
		$("#productName").text(productName);
		//原单价
		$("#productPrice").text('原价￥'+parseFloat(productPrice));
		//原单价查看
		$("#buyNumView").text(buyNum);
		//调整后价格小计
		$("#productItemMoney").text('￥'+parseFloat(multiply(subtraction(productPrice, adjustPrice), buyNum)));
		$('#edit_pricebox').toggle();
	});
	
	$(".icon_close").click(function(){
		$('#edit_pricebox').toggle();
	});
	
	$("#btn_submit").click(function(){
		submitAdjustProductPrice();
	});
	
	// 调价保存
	$('#btn_confirm_adjust_price').on('click', function() {
		
		// 提交订单号  商品调价  订单调价  运费
		
		// 订单号
		var orderCode = $("#orderCode").val();
		var produtAdjustPrice = [];
		var ids = [];
		var buyNums = [];
		var produtPrice = [];
		
		// 商品调价
		$("input[name='adjustProductPrice']").each(function(i,item){
			var productAdjustPrice = $(this).val() ? $(this).val() : 0;
			produtAdjustPrice.push(productAdjustPrice);
		});
		
		// 商品ids
		$("input[name='id']").each(function(i,item){
			ids.push($(this).val());
		});
		
		// 购买数量
		$("input[name='buyNum']").each(function(i,item){
			buyNums.push($(this).val());
		});
		
		// 商品原价
		$("input[name='productPrice']").each(function(i,item){
			var productPrice = $(this).val() ? $(this).val() : 0;
			produtPrice.push(productPrice);
		});
		
		// 运费
		var deliveryFeeMoney = $("#deliveryFeeMoney").val() ? $("#deliveryFeeMoney").val() : 0;
		// 订单调价
		var orderAdjustMoney = $("#orderAdjustPrice").val() ? $("#orderAdjustPrice").val() : 0;
		// 调价原因
		var adjustReason = $("#adjustReason").val();
		
		var params = {
			orderCode : orderCode,
			ids : ids.join(','),
			produtAdjustPrice: produtAdjustPrice.join(','),
			produtPrice : produtPrice.join(','),
			deliveryFeeMoney: deliveryFeeMoney,
			orderAdjustMoney: orderAdjustMoney,
			buyNums: buyNums.join(','),
			adjustReason : adjustReason
		};
		
		var url = rootPath + "/wx/order/adjustOrderPrice";
		$.ajax({
	        type:"post",
	        url:url,
	        data:params,
	        datatype: "json",
	        success:function(json){
	        	if(json.resultCode == '1'){
	        		showMsg(json.resultMsg);
	        		setTimeout(function(){
	        			window.location.reload();
	        		}, 500);
	        	}
	        },
	        error: function(){
	        	showErr(json.resultMsg);
	        }
		});
		
	});
})

function adjustPrice(tag){
	var adjustPrice = $("#adjustPrice").val();
	if (tag == 'plus') {
		$("#plus").addClass("current");
		$("#minus").removeClass("current");
		$("#adjustPrice").val(addition(adjustPrice, 1));
	} else {
		$("#plus").removeClass("current");
		$("#minus").addClass("current");
		$("#adjustPrice").val(subtraction(adjustPrice, 1));
	}
	changePrice();
}

//商品价格变更
function changePrice(){
	//商品单价信息
	var detailId = $("#detailId").val();
	var productPrice = $("input[tagId='"+detailId+"'][name='productPrice']").val();
	var adjustPrice = $("#adjustPrice").val();
	var buyNum = $("input[tagId='"+detailId+"'][name='buyNum']").val();
	
	//小计价格(单一商品总价)
	var productItemMoney = multiply(subtraction(productPrice, adjustPrice), buyNum);
	$("#productItemMoney").text('￥'+parseFloat(productItemMoney));
}

//确认调整商品价格(非提交)
function submitAdjustProductPrice(){
	//商品单价信息
	var detailId = $("#detailId").val();
	var productPrice = $("input[tagId='"+detailId+"'][name='productPrice']").val();
	var adjustPrice = $("#adjustPrice").val();
	var buyNum = $("input[tagId='"+detailId+"'][name='buyNum']").val();
	
	//调整后商品单价
	var adjustProductPrice = subtraction(productPrice, adjustPrice);
	$("input[tagId='"+detailId+"'][name='adjustProductPrice']").val(adjustProductPrice);
	
	//调整后商品价格填充
	var u = "调价后<u>￥"+parseFloat(adjustProductPrice)+"</u>";
	if (parseFloat(adjustProductPrice) == parseFloat(productPrice)) {
		u = "原价<u>￥"+parseFloat(productPrice)+"</u>";
	}
	$("strong[tagId='"+detailId+"'][tag='adjustPriceSpan']").find("span").html(u);
	$("#edit_pricebox").toggle();
	changeTotalProductAdjustPrice();
}

//商品调价计算
function changeTotalProductAdjustPrice() {
	var totalDiffMoney = 0.00;
	$("strong[tag='adjustPriceSpan']").each(function(){
		//原价
		var productPrice = $(this).find("input[name='productPrice']").val();
		//现价
		var adjustPorductPrice = $(this).find("input[name='adjustProductPrice']").val();
		//数量
		var buyNum = $(this).find("input[name='buyNum']").val();
		//单种商品调整价格
		var adjustPrice = multiply(subtraction(productPrice, adjustPorductPrice), buyNum);
		totalDiffMoney = addition(totalDiffMoney, adjustPrice);
	});
	$("#totalProductAdjustMoneyShow").text("-￥"+totalDiffMoney);
	$("#totalProductAdjustMoney").val(totalDiffMoney);
	realPayMoney();
}

//订单调价
function orderPriceAdjust() {
	var orderAdjustPrice = $("#orderAdjustPrice").val() ? $("#orderAdjustPrice").val() : 0;
	$("#orderAdjustPriceShow").text("-￥"+orderAdjustPrice);
	realPayMoney();
}

//输入运费需要重新计算价格
function addDeliveryFeeMoney(){
	realPayMoney()
}

//调价后总价
function realPayMoney() {
	//商品总价
	var totalProductMoney = $("#totalProductMoney").val();
	//运费
	var deliveryFeeMoney =$("#deliveryFeeMoney").val();
	//订单调价
	var orderAdjustPrice = $("#orderAdjustPrice").val();
	//商品总调价
	var totalProductAdjustMoney = $("#totalProductAdjustMoney").val();
	var realPayMoney = addition(totalProductMoney, deliveryFeeMoney);
	realPayMoney = addition(realPayMoney, orderAdjustPrice);
	realPayMoney = subtraction(realPayMoney, totalProductAdjustMoney);
	$("#realPayMoneyShow").text(realPayMoney);
}

/**
 * 四舍五入
 * @param num 数字
 * @param v 保留位数
 * @returns {Number}
 */
function decimal(num,v) {
	var vv = Math.pow(10,v);
	return Math.round(parseFloat(num)*vv)/vv;
}

/**
 * 加法
 * @param num1
 * @param num2
 */
function addition(num1, num2) {
	num1 = isNaN(parseFloat(num1)) ? 0 : parseFloat(num1);
	num2 = isNaN(parseFloat(num2)) ? 0 : parseFloat(num2);
	return Math.round((num1*100)+(num2*100))/100;
}

/**
 * 减法
 * @param num1
 * @param num2
 * @returns {Number}
 */
function subtraction(num1, num2) {
	num1 = isNaN(parseFloat(num1)) ? 0 : parseFloat(num1);
	num2 = isNaN(parseFloat(num2)) ? 0 : parseFloat(num2);
	return Math.round((num1*100)-(num2*100))/100;
}

/***
 * 乘法
 * @param num1
 * @param num2
 */
function multiply(num1, num2) {
	num1 = isNaN(parseFloat(num1)) ? 0 : parseFloat(num1);
	num2 = isNaN(parseFloat(num2)) ? 0 : parseFloat(num2);
	return Math.round((num1*100)*(num2*100))/10000;
}

